{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# curious_set_of_integers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/curious_set_of_integers.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/curious_set_of_integers.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Crypto problem in Google CP Solver.\n",
    "\n",
    "  Martin Gardner (February 1967):\n",
    "  '''\n",
    "  The integers 1,3,8, and 120 form a set with a remarkable property: the\n",
    "  product of any two integers is one less than a perfect square. Find\n",
    "  a fifth number that can be added to the set without destroying\n",
    "  this property.\n",
    "  '''\n",
    "\n",
    "  Solution: The number is 0.\n",
    "\n",
    "  There are however other sets of five numbers with this property.\n",
    "  Here are the one in the range of 0.10000:\n",
    "  [0, 1, 3, 8, 120]\n",
    "  [0, 1, 3, 120, 1680]\n",
    "  [0, 1, 8, 15, 528]\n",
    "  [0, 1, 8, 120, 4095]\n",
    "  [0, 1, 15, 24, 1520]\n",
    "  [0, 1, 24, 35, 3480]\n",
    "  [0, 1, 35, 48, 6888]\n",
    "  [0, 2, 4, 12, 420]\n",
    "  [0, 2, 12, 24, 2380]\n",
    "  [0, 2, 24, 40, 7812]\n",
    "  [0, 3, 5, 16, 1008]\n",
    "  [0, 3, 8, 21, 2080]\n",
    "  [0, 3, 16, 33, 6440]\n",
    "  [0, 4, 6, 20, 1980]\n",
    "  [0, 4, 12, 30, 5852]\n",
    "  [0, 5, 7, 24, 3432]\n",
    "  [0, 6, 8, 28, 5460]\n",
    "  [0, 7, 9, 32, 8160]\n",
    "\n",
    "\n",
    "  Compare with the following models:\n",
    "  * MiniZinc: http://www.hakank.org/minizinc/crypta.mzn\n",
    "  * Comet   : http://www.hakank.org/comet/crypta.co\n",
    "  * ECLiPSe : http://www.hakank.org/eclipse/crypta.ecl\n",
    "  * SICStus : http://hakank.org/sicstus/crypta.pl\n",
    "\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "\n",
    "def decreasing(solver, x):\n",
    "  for i in range(len(x) - 1):\n",
    "    solver.Add(x[i] <= x[i + 1])\n",
    "\n",
    "\n",
    "def main():\n",
    "\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver(\"Curious set of integers\")\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  n = 5\n",
    "  max_val = 10000\n",
    "\n",
    "  #\n",
    "  # variables\n",
    "  #\n",
    "  x = [solver.IntVar(0, max_val, \"x[%i]\" % i) for i in range(n)]\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "  solver.Add(solver.AllDifferent(x))\n",
    "  decreasing(solver, x)\n",
    "\n",
    "  for i in range(n):\n",
    "    for j in range(n):\n",
    "      if i != j:\n",
    "        p = solver.IntVar(0, max_val, \"p[%i,%i]\" % (i, j))\n",
    "        solver.Add(p * p - 1 == (x[i] * x[j]))\n",
    "\n",
    "  # This is the original problem:\n",
    "  # Which is the fifth number?\n",
    "  v = [1, 3, 8, 120]\n",
    "  b = [solver.IsMemberVar(x[i], v) for i in range(n)]\n",
    "  solver.Add(solver.Sum(b) == 4)\n",
    "\n",
    "  #\n",
    "  # search and result\n",
    "  #\n",
    "  db = solver.Phase(x, solver.CHOOSE_MIN_SIZE_LOWEST_MIN,\n",
    "                    solver.ASSIGN_MIN_VALUE)\n",
    "\n",
    "  solver.NewSearch(db)\n",
    "\n",
    "  num_solutions = 0\n",
    "  while solver.NextSolution():\n",
    "    num_solutions += 1\n",
    "    print(\"x:\", [int(x[i].Value()) for i in range(n)])\n",
    "\n",
    "  solver.EndSearch()\n",
    "\n",
    "  print()\n",
    "  print(\"num_solutions:\", num_solutions)\n",
    "  print(\"failures:\", solver.Failures())\n",
    "  print(\"branches:\", solver.Branches())\n",
    "  print(\"WallTime:\", solver.WallTime())\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
